﻿//-----------------------------------------------------------------------
// <copyright file="DriveExplorerView.xaml.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Model.Entities;
using ConsoleHoster.ViewModel;
using ConsoleHoster.ViewModel.Entities;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ConsoleHoster.View.Controls
{
	/// <summary>
	/// Interaction logic for DriveExplorerView.xaml
	/// </summary>
	public partial class DriveExplorerView : UserControl
	{
		public DriveExplorerView()
		{
			InitializeComponent();
		}

		private void UserControl_DataContextChanged_1(object sender, DependencyPropertyChangedEventArgs e)
		{
			ExplorerViewModel tmpOldVM = e.OldValue as ExplorerViewModel;
			if (tmpOldVM != null)
			{
				tmpOldVM.SaveState();
			}

			ExplorerViewModel tmpNewVM = e.NewValue as ExplorerViewModel;
			if (tmpNewVM != null)
			{
				tmpNewVM.RestoreState();
			}
		}

		private void OnExplorerItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			ExplorerItemViewModel tmpVM = GetViewModelForSender(sender);
			if (tmpVM != null && tmpVM.HandleMouseInput(MouseButton.Left, MouseButtonState.Pressed, e.ClickCount, null))
			{
				e.Handled = true;
			}
		}

		private void OnExplorerItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			ExplorerItemViewModel tmpVM = GetViewModelForSender(sender);
			if (tmpVM != null && tmpVM.HandleMouseInput(MouseButton.Left, MouseButtonState.Released, e.ClickCount, null))
			{
				e.Handled = true;
			}
		}

		private void OnExplorerItem_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
		{
			ExplorerItemViewModel tmpVM = GetViewModelForSender(sender);
			if (tmpVM != null && tmpVM.HandleMouseInput(MouseButton.Right, MouseButtonState.Released, e.ClickCount, null))
			{
				e.Handled = true;
			}
		}

		private void OnContextMenu_OpenExplorer(object sender, RoutedEventArgs e)
		{
			ExplorerItemViewModel tmpItem = (sender as MenuItem).DataContext as ExplorerItemViewModel;
			if (tmpItem != null)
			{
				tmpItem.OpenExplorer();
				e.Handled = true;
			}
		}

		private void OnContextMenu_UsePath(object sender, RoutedEventArgs e)
		{
			ExplorerItemViewModel tmpItem = (sender as MenuItem).DataContext as ExplorerItemViewModel;
			if (tmpItem != null)
			{
				this.ViewModel.OnItemChosen(tmpItem.ExplorerItem, true);
				e.Handled = true;
			}
		}

		private void OnContextMenu_UseName(object sender, RoutedEventArgs e)
		{
			ExplorerItemViewModel tmpItem = (sender as MenuItem).DataContext as ExplorerItemViewModel;
			if (tmpItem != null)
			{
				this.ViewModel.OnItemChosen(tmpItem.ExplorerItem, false);
				e.Handled = true;
			}
		}

		private void OnContextMenu_RedirectTo(object sender, RoutedEventArgs e)
		{
			ExplorerItemViewModel tmpItem = (sender as MenuItem).DataContext as ExplorerItemViewModel;
			if (tmpItem != null && tmpItem.OnFolderChanged())
			{
				e.Handled = true;
			}
		}

		private void OnContextMenu_Favorite(object sender, RoutedEventArgs e)
		{
			ExplorerItemViewModel tmpItem = (sender as MenuItem).DataContext as ExplorerItemViewModel;
			if (tmpItem != null)
			{
				tmpItem.OnCreateFavorite();
				e.Handled = true;
			}
		}

		private void OnContextMenu_Run(object sender, RoutedEventArgs e)
		{
			ExplorerItemViewModel tmpItem = (sender as MenuItem).DataContext as ExplorerItemViewModel;
			if (tmpItem != null && tmpItem.Run())
			{
				e.Handled = true;
			}
		}

		private static ExplorerItemViewModel GetViewModelForSender(object sender)
		{
			return (sender as FrameworkElement).DataContext as ExplorerItemViewModel;
		}

		private ExplorerViewModel ViewModel
		{
			get
			{
				return this.DataContext as ExplorerViewModel;
			}
		}

		public string SelectedImagePath
		{
			get;
			set;
		}
	}
}